/*
 * @Description: 新增更新日志弹窗组件
 * @Author: Rfan
 * @Date: 2022-06-11 09:59:07
 * @LastEditTime: 2022-06-11 15:16:01
 */

import { useEffect } from 'react';
import { Button, Form, Modal } from 'antd';
import type { FC } from 'react';
import { useChangeLogContext } from '../../contexts/ChangeLogContext';
import ChangeLogFormItem from './common/ChangeLogFormItem';
import type { ICommonDialog } from '@/interfaces/global';

interface AddDialogProps extends ICommonDialog {}

const AddDialog: FC<AddDialogProps> = (props) => {
  const { visible, onClose } = props;
  const { onAddChangeLog } = useChangeLogContext();
  const [form] = Form.useForm();

  useEffect(() => {
    if (visible) {
      //
    }
  }, [visible]);
  // 提交表单
  const submitFunc = () => {
    form.submit();
  };

  // 新增排序方法
  const onFinish = (values: any) => {
    console.log({ values });
    onAddChangeLog(values).then(() => {
      onCancel();
    });
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log('Failed:', errorInfo);
  };

  const onCancel = () => {
    onClose();
  };

  return (
    <Modal
      destroyOnClose
      title="新增更新日志"
      visible={visible}
      width={650}
      onCancel={onCancel}
      footer={[
        <Button key="back" onClick={onCancel}>
          取消
        </Button>,
        <Button key="submit" type="primary" onClick={submitFunc}>
          确定
        </Button>
      ]}
    >
      <Form
        labelCol={{ span: 6 }}
        wrapperCol={{ span: 16 }}
        preserve={false}
        form={form}
        onFinish={onFinish}
        onFinishFailed={onFinishFailed}
      >
        <ChangeLogFormItem />
      </Form>
    </Modal>
  );
};

export default AddDialog;
